'use strict'
import React from 'react';
import {Dimensions, PixelRatio} from 'react-native';

var util = {
    // apiUrl: 'http://192.168.2.210:7700',//公司开发
    apiUrl: 'http://139.224.9.151:8080/zyd',//外网服务器
    // apiUrl: 'http://192.168.1.104:7700',//家里开发
    qiniuUrl: 'http://oybd6m8je.bkt.clouddn.com/',//七牛云空间
    qiniuDefault: '!default',//七牛默认样式
    qiniu200Url: '!200px',//七牛200尺寸的图片样式
    qiniu500Url: '!500',//七牛500尺寸的图片样式
    deploymentProKey: 'Er-wwLH7Lc_oLhFd0A4soNgLceLq244bf450-c39d-428a-8e1d-eb96a5ce93f9',
    deploymentStagKey: 'UcOXWl4RuZyCCMG3giu0R_7nS2bq244bf450-c39d-428a-8e1d-eb96a5ce93f9',
    uiWidth: 375,
    uiHeight: 667,
    pixel: 1 / PixelRatio.get(),
    screenWidth: Dimensions.get('window').width,
    screenHeith: Dimensions.get('window').height,
    pixelRatio: PixelRatio.get(),
    fontScale: PixelRatio.getFontScale(),
    scale: Math.min(Dimensions.get('window').height / 667, Dimensions.get('window').width / 375),

    /*宽度适配*/
    autoWidth: function (value) {
        return Dimensions.get('window').width * value / this.uiWidth;
    },
    /*高度适配*/
    autoheight: function (value) {
        return Dimensions.get('window').height * value / this.uiHeight;
    },
    get: function (url, successCallback, failCallback) {
        fetch(url).then((response) => response.text())
            .then((responseText) => {
                successCallback(JSON.parse(responseText));
            }).catch(function (err) {
            failCallback(err);
        });
    },
    /*字体大小适配*/
    setSpText: function (number) {
        number = Math.round((number * this.scale + 0.5) * this.pixelRatio / this.fontScale);
        return number / PixelRatio.get();
    },
    /*通过value删除数组元素*/
    removeByValue: function (arr, value) {
        let i = arr.length;
        while (i--) {
            if (arr[i] === value) {
                arr.splice(i, 1);
            }
        }
    },
    /*判断是否存在数组*/
    isInArray: function (arr, value) {
        let i = arr.length;
        while (i--) {
            if (arr[i] === value) {
                return true
            }
        }
    },
    /*格式化数字，两位小数*/
    toDecimal2: function (x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x * 100) / 100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    },
};

module.exports = util;
